package com.jinlejia.erp.adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.freedom.yefeng.yfrecyclerview.YfListAdapter
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder
import com.jinlejia.erp.R
import com.jinlejia.erp.databinding.ItemMessageBinding
import com.wang.avi.AVLoadingIndicatorView
import java.text.SimpleDateFormat
import java.util.*


/**
 * 消息（Adapter）
 */

class MessageAdapter(data: ArrayList<Map<String, Any>>) : YfListAdapter<Map<String, Any>>(data) {

    private var context: Context? = null

    override fun onCreateDataViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        context = parent.context.applicationContext
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false)
        return ViewHolder(view)
    }

    override fun onCreateEmptyViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.empty_layout, parent, false)
        val txt = view.findViewById<TextView>(R.id.id_tips)
        txt.text = "暂无数据"
        return YfSimpleViewHolder(view)
    }

    @Suppress("RedundantOverride")
    override fun setData(data: ArrayList<Map<String, Any>>) {
        super.setData(data)
    }

    override fun onCreateErrorViewHolder(parent: ViewGroup): RecyclerView.ViewHolder? {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.error_layout, parent, false)
        return YfSimpleViewHolder(view)
    }

    override fun onBindDataViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<ItemMessageBinding>(viewHolder.itemView)!!
        val map = mData[position]
        val sdf = SimpleDateFormat("yyyy-MM-dd")

        if (map.containsKey("title")) {
            binding.title.text = map["title"].toString()
        }

        if (map.containsKey("remindTime")) { //时间
            binding.time.text =
                sdf.format(Date(java.lang.Long.valueOf(mData[position]["remindTime"].toString())))
        }
        if (map.containsKey("content")) { //内容
            binding.message.text = mData[position]["content"].toString()
        }
        if (map.containsKey("unReadCount")) { //未读数
            binding.msgCount.text = mData[position]["unReadCount"].toString()
            if (!TextUtils.isEmpty(mData[position]["unReadCount"].toString()) && mData[position]["unReadCount"].toString() == "0") {
                binding.msgCount.visibility = View.GONE
            } else {
                binding.msgCount.visibility = View.VISIBLE
                if (Integer.parseInt(mData[position]["unReadCount"].toString()) > 99) {
                    binding.msgCount.text = "99+"
                }
            }
        }
        binding.root.tag = mData[position]
    }

    override fun onCreateLoadingViewHolder(parent: ViewGroup): RecyclerView.ViewHolder? {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.loading_layout, parent, false)
        return YfSimpleViewHolder(view)
    }

    override fun onCreateFooterViewHolder(parent: ViewGroup): RecyclerView.ViewHolder? {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.footer_loading, parent, false)
        return FooterViewHolder(view)
    }

    override fun onBindFooterViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        super.onBindFooterViewHolder(holder, position)
        if (mFooters[0] == R.string.is_all) {
            (holder as FooterViewHolder).txt.setText(R.string.is_all)
            holder.txt.visibility = View.VISIBLE
            holder.progress.visibility = View.GONE
        } else {
            (holder as FooterViewHolder).txt.visibility = View.GONE
            holder.progress.visibility = View.VISIBLE
        }
    }

    internal class FooterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var txt: TextView = itemView.findViewById(R.id.txt)
        var progress: AVLoadingIndicatorView = itemView.findViewById(R.id.progress)
    }

    internal class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        init {
            DataBindingUtil.bind<ItemMessageBinding>(view)
        }
    }
}
